package com.fengchunge.util;

import java.math.BigDecimal;

/**
 * @description: 商业运算处理货币的工具类，使用时需要使用BigDecimal的String构造器
 * @author: mingtai
 * @createDate: 23:23 2020/3/31
 * @version: 1.0
 */

public class BigDecimalUtil {

    private BigDecimalUtil() {
        //私有化构造，不让外部new对象
    }

    public static BigDecimal add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2);
    }

    public static BigDecimal sub(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2);
    }

    public static BigDecimal mul(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2);
    }

    public static BigDecimal div(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP); //除不尽时，四舍五入，保留两位小数。
    }
}



